<?php


class Image
{


   
   function __construct()
   {
        $this->Image();
   }

   function Image()
   {
        
   }

/**
 * 利用gd库生成缩略图
 *
 * @author  weberliu
 * @param   string      $src            原图片路径
 * @param   string      $dst            缩略图保存路径
 * @param   int         $thumb_width    缩略图高度
 * @param   int         $thumb_height   缩略图高度 可选
 * @param   int         $quality        缩略图品质 100之内的正整数
 * @return  boolean     成功返回 true 失败返回 false
 */
  function make_thumb($src, $dst, $thumb_width, $thumb_height = 0, $quality = 85)
  {
    if (function_exists('imagejpeg'))
    {
        $func_imagecreate = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate';
        $func_imagecopy = function_exists('imagecopyresampled') ? 'imagecopyresampled' : 'imagecopyresized';
        $dirpath = dirname($dst);
        if (!get_dir($dirpath, 0777))
        {
            return false;
        }

        $data = getimagesize($src);
        $src_width = $data[0];
        $src_height = $data[1];
        if ($thumb_height == 0)
        {
            if ($src_width > $src_height)
            {
                $thumb_height = $src_height * $thumb_width / $src_width;
            }
            else
            {
                $thumb_height = $thumb_width;
                $thumb_width = $src_width * $thumb_height / $src_height;
            }
            $dst_x = 0;
            $dst_y = 0;
            $dst_w = $thumb_width;
            $dst_h = $thumb_height;
        }
        else
        {
            if ($src_width / $src_height > $thumb_width / $thumb_height)
            {
                $dst_w = $thumb_width;
                $dst_h = ($dst_w * $src_height) / $src_width;
                $dst_x = 0;
                $dst_y = ($thumb_height - $dst_h) / 2;
            }
            else
            {
                $dst_h = $thumb_height;
                $dst_w = ($src_width * $dst_h) / $src_height;
                $dst_y = 0;
                $dst_x = ($thumb_width - $dst_w) / 2;
            }
        }

        switch ($data[2])
        {
            case 1:
                $im = imagecreatefromgif($src);
                break;
            case 2:
                $im = imagecreatefromjpeg($src);
                break;
            case 3:
                $im = imagecreatefrompng($src);
                break;
            default:
                trigger_error("Cannot process this picture format: " .$data['mime']);
                break;
        }
        $ni = $func_imagecreate($thumb_width, $thumb_height);
        if ($func_imagecreate == 'imagecreatetruecolor')
        {
            imagefill($ni, 0, 0, imagecolorallocate($ni, 255, 255, 255));
        }
        else
        {
            imagecolorallocate($ni, 255, 255, 255);
        }
        $func_imagecopy($ni, $im, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height);
        imagejpeg($ni, $dst, $quality);
        return is_file($dst) ? $dst : false;
    }
    else
    {
        trigger_error("Unable to process picture.", E_USER_ERROR);
    }
 }
/**
 * 给图片添加水印
 * @param filepath $src 待处理图片
 * @param filepath $mark_img 水印图片路径
 * @param string $position 水印位置 lt左上  rt右上  rb右下  lb左下 其余取值为中间
 * @param int $quality jpg图片质量，仅对jpg有效 默认85 取值 0-100之间整数
 * @param int $pct 水印图片融合度(透明度)
 *
 * @return void
 */
function water_mark($src, $mark_img, $position = 'rb', $quality = 85, $pct = 80) 
{
    if(function_exists('imagecopy') && function_exists('imagecopymerge')) {
        $data = getimagesize($src);
        if ($data[2] > 3)
        {
            return false;
        }
        $src_width = $data[0];
        $src_height = $data[1];
        $src_type = $data[2];

        $data = getimagesize($mark_img);
        $mark_width = $data[0];
        $mark_height = $data[1];
        $mark_type = $data[2];

        if ($src_width < ($mark_width + 20) || $src_width < ($mark_height + 20))
        {
            return false;
        }
        switch ($src_type)
        {
            case 1:
                $src_im = imagecreatefromgif($src);
                $imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
                break;
            case 2:
                $src_im = imagecreatefromjpeg($src);
                $imagefunc = function_exists('imagegif') ? 'imagejpeg' : '';
                break;
            case 3:
                $src_im = imagecreatefrompng($src);
                $imagefunc = function_exists('imagepng') ? 'imagejpeg' : '';
                break;
        }
        switch ($mark_type)
        {
            case 1:
                $mark_im = imagecreatefromgif($mark_img);
                break;
            case 2:
                $mark_im = imagecreatefromjpeg($mark_img);
                break;
            case 3:
                $mark_im = imagecreatefrompng($mark_img);
                break;
        }

        switch ($position)
        {
            case 'lt':
                $x = 10;
                $y = 10;
                break;
            case 'rt':
                $x = $src_width - $mark_width - 10;
                $y = 10;
                break;
            case 'rb':
                $x = $src_width - $mark_width - 10;
                $y = $src_height - $mark_height - 10;
                break;
            case 'lb':
                $x = 10;
                $y = $src_height - $mark_height - 10;
                break;
            default:
                $x = ($src_width - $mark_width - 10) / 2;
                $y = ($src_height - $mark_height - 10) / 2;
                break;
        }

        if (function_exists('imagealphablending')) imageAlphaBlending($mark_im, true);
        imageCopyMerge($src_im, $mark_im, $x, $y, 0, 0, $mark_width, $mark_height, $pct);

        if ($src_type == 2)
        {
            $imagefunc($src_im, $src, $quality);
        }
        else
        {
            $imagefunc($src_im, $src);
        }
    }
}

//认证码
function check_img($nmsg='')
{

         header("Cache-Control: no-cache, must-revalidate");
         header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
         header("Content-type: image/jpeg");
		
         $numberOfCharacters = strlen(trim($nmsg));//显示几个字符
         $string = $nmsg;//要显示的字符串

         $font = realpath(ROOT_PATH."/data/fontstyle/exagger8.otf");
         $imgX = 60 + 30 * $numberOfCharacters;
         $imgY = 80;
         $img = @imagecreatetruecolor($imgX, $imgY) or die("Cannot Initialize new GD image stream");

         $background = imagecolorallocate($img, rand(110, 150), rand(110, 150), rand(110, 150));
         imagefill($img, 0, 0, $background);

         for ($i = 0; $i < 3; $i++)
		 {
     	 $color = imagecolorallocate($img, rand(110, 150), rand(110, 150), rand(110, 150));
	     imagefilledellipse($img, rand(0, $imgX), rand(0, $imgY), rand(25, $imgX), rand(25, $imgY), $color);
         }

         $length = strlen($string);
         for ($i = 0; $i < $length; $i++) 
		 {
	     $shadow = imagecolorallocate($img, rand(100, 125), rand(100, 125), rand(100, 125));
	     $color = imagecolorallocate($img, rand(50, 100), rand(50, 100), rand(50, 100));
	     $angle = rand(-30, 30);
	     imagettftext($img, 40, $angle, $i * 35 + 20 + 3, 50 + 3, $shadow, $font, $string[$i]);
	     imagettftext($img, 40, $angle, $i * 35 + 20, 50, $color, $font, $string[$i]);
         }

         // Output the image!
         imagejpeg($img, null, 80);
         imagedestroy($img);
}

//原图,处理过的图,坐标原点X,坐标原点Y,最终图宽,最终图高,末坐标X,末坐标Y,原图缩放情况,是否自动取值
function cutimg($srcimgurl,$endimgurl,$x,$y,$endimg_w,$endimg_h,$border_w,$border_h,$scale=100,$fix=0)
{
	$path = dirname ($endimgurl);
	if (!is_dir($path))
	{
		if(!@mkdir ($path, 0777))
		{
			showerr("{$path} 此目录不能创建,文件创建失败");
		}
	}
	$ground_info = getimagesize($srcimgurl);
	switch($ground_info[2])
	{ 
		case 1:$im = imagecreatefromgif($srcimgurl);break; 
		case 2:$im = imagecreatefromjpeg($srcimgurl);break; 
		case 3:$im = imagecreatefrompng($srcimgurl);break; 
		default:die("图片格式不允许$srcimgurl"); 
    }
	if($fix)
	{//方便截取头像的一部分
		if($ground_info[0]<$ground_info[1])
		{
			$border_w=$ground_info[0];
			$border_h=$endimg_h*$ground_info[0]/$endimg_w;
		}elseif($ground_info[0]>$ground_info[1])
		{
			$border_h=$ground_info[1];
			$border_w=$endimg_w*$ground_info[1]/$endimg_h;
		}else
		{
			$border_w=$ground_info[0];
			$border_h=$ground_info[1];
		}
	}
	$newim = imagecreatetruecolor($endimg_w, $endimg_h);
	$x=($x*100)/$scale;
	$y=($y*100)/$scale;
	$border_width=($border_w*100)/$scale;
	$border_height=($border_h*100)/$scale;
	imagecopyresampled($newim, $im, 0,0, $x,$y, $endimg_w, $endimg_h, $border_width, $border_height );
	if(function_exists("Imagegif"))
	{
		switch($ground_info[2])
		{ 
			case 1:imagegif($newim,$endimgurl);break;
			case 2:imagejpeg($newim,$endimgurl);break;
			case 3:imagepng($newim,$endimgurl);break;
			default:die("errorMsg"); 
		}
	}elseif(function_exists("imagejpeg"))
	{
		imagejpeg($newim,$endimgurl);
	}else
	{
		imagepng($newim,$endimgurl);
	}
	ImageDestroy ($newim);
	ImageDestroy ($im);
}


}







?>